(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.companyRating.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'companyRatingService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, companyRatingService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {}, oldNotifyTplDetails = [];
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companyRating = {};
		
		
		ctrl.init = function() {
			
			authService.update(params).then(function(response) {
				ctrl.companyRating = response.data || params;
				companyRatingService.initRatingTypeQaItemAndQaAdj(ctrl, layoutConfig);
			});
		};

		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.companyRating.taskId) {
				return authService.updateContinueSave(ctrl.companyRating).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.companyRating).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.companyRating.taskId) {
				return authService.updateContinueSubmit(ctrl.companyRating).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.companyRating).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.addNotifyTplDetail = function() {
			if (!ctrl.companyRating.ratingTypes) {
				ctrl.companyRating.ratingTypes = [];
			}
			var item = {
				eventCode : ctrl.companyRating.eventCode,
				localeCode : ctrl.companyRating.localeCode,
				companyRatingId : ctrl.companyRating.companyRatingId,
				notifyType : '',
				companyType : ''
			};
			item.dataStatus = 'C';
			ctrl.companyRating.ratingTypes.push(item);
		};

		ctrl.removeNotifyTplDetail = function() {
			angular.forEach(ctrl.selectedNotifyTplDetails, function(selectedItem, index) {
				angular.forEach(ctrl.companyRating.ratingTypes, function(item, i) { 
					if (selectedItem.$$hashKey == item.$$hashKey) { 
						if('C' == selectedItem.dataStatus){
							ctrl.companyRating.ratingTypes.splice(i, 1);
						} else {
							ctrl.companyRating.ratingTypes[i].dataStatus = 'D';
						}
					}
				});
			});
		};
		ctrl.notifyTypeChanged = function(obj, form) {
			if(form.$dirty && 'C' != obj.dataStatus) obj.dataStatus = 'U';
		};
		ctrl.companyTypeChanged = function(obj, form) {
			if(form.$dirty && 'C' != obj.dataStatus) obj.dataStatus = 'U';
		};
		$scope.$on("COMPANYTYPE2_CANCEL", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		$scope.$on("COMPANYTYPE2_OK", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedNotifyTplDetails = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.companyRating.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.companyrating.list',
					'state' : 'cda.companyRating.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
